package com.base.cn.platform.os.controller.web.index;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.md5.MD5;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.count.PageFrom;
import com.base.cn.platform.os.common.utils.count.PageKeyUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.common.utils.web.WebUtils;
import com.base.cn.platform.os.service.course.open.CourseOpenService;
import com.base.cn.platform.os.service.manage.count.PageViewCountService;
import com.base.cn.platform.os.service.manage.op.advert.OpAdvertService;
import com.base.cn.platform.os.service.manage.op.groom.OpGroomDataService;
import com.base.cn.platform.os.service.manage.planTools.PopupSloganDetailService;
import com.base.cn.platform.os.service.manage.planTools.PopupSloganService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页Controller
 *
 * @author s.li
 * @create 2018-05-10-19:35
 */
@Controller
public class WebIndexController extends WebBaseController {

    @Autowired
    private OpGroomDataService opGroomDataService;
    @Autowired
    private OpAdvertService opAdvertService;
    @Autowired
    private CourseOpenService courseOpenService;
    @Autowired
    private PageViewCountService pageViewCountService;
    @Autowired
    private PopupSloganService popupSloganService;
    @Autowired
    private PopupSloganDetailService popupSloganDetailService;

    @RequestMapping("/web/index")
    public String index(){
        pageViewCountService.addPageCount(PageKeyUtils.INDEX, PageFrom.PAGE_FROM_PC);
        return resultPage("/index/index");
    }

    @RequestMapping("/")
    public String home(){
        return "redirect:/web/index";
    }

    @RequestMapping("/web/webError")
    public String error(){
        return "/common/error";
    }

    /**
     * web首页banner查询
     * @return String
     */
    @RequestMapping("/web/index/bannerList")
    public String bannerList(Model model){
        Map<String, Object> params = new HashMap<>();
        params.put("placeKey", "INDEX");
        params.put("status",1);
        List<Map<String, Object>> advertList = opAdvertService.queryAdvertList(params, false, false, false);
        model.addAttribute("advertList", advertList);
        return resultPage("/index/index-banner");
    }

    /**
     * PC端首页查询推荐数据
     * @param model
     * @param placeKey
     * @return
     */
    @RequestMapping("/web/index/queryGroomDataList")
    public String queryGroomDataList(Model model,
                                     @RequestParam(value = "placeKey") String placeKey){
        Map<String, Object> params = new HashMap<>();
        params.put("placeKey", placeKey);
        List<Map<String, Object>> dataList = opGroomDataService.queryGroomDataList(params);
        model.addAttribute("dataList", dataList);
        if("IBC".equalsIgnoreCase(placeKey) || "IHC".equalsIgnoreCase(placeKey) || "IRP".equalsIgnoreCase(placeKey)){
            return resultPage("/index/groom-course-list");
        }else if("IPT".equalsIgnoreCase(placeKey)){//推荐讲师
            return resultPage("/index/groom-teacher-list");
        }else if("IRB".equalsIgnoreCase(placeKey)){//推荐图书
            return resultPage("/index/groom-book-list");
        }else{//推荐资讯
            return resultPage("/index/groom-article-list");
        }
    }

    /**
     * 首页查询免费直播列表
     * @param model
     * @return
     */
    @RequestMapping("/web/index/findCourseOpenList")
    public String findCourseOpenList(Model model, HttpServletRequest request){
        Map<String, Object> params = new HashMap<>();
        params.put("count", 12);
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        params.put("userId",userId);
        params.put("status",1);
        List<Map<String, Object>> courseOpenList = courseOpenService.findCourseOpenList(params, false,  true, false,true);
        model.addAttribute("courseOpenList", courseOpenList);
        return resultPage("/index/groom-course-open-list");
    }

    /**
     * web首页banner查询
     * @return String
     */
    @RequestMapping("/web/index/savePageView")
    @ResponseBody
    public void savePageView(){
        pageViewCountService.addPageCount(PageKeyUtils.ACTIVITY_INFO, PageFrom.PAGE_FROM_PC);
    }

    /**
     * 获取是否有弹框
     */
    @RequestMapping("/web/index/getIndexPopupSlogan")
    @ResponseBody
    public Map getPopupSlogan(HttpServletRequest request, HttpServletResponse response) {
        String userCookie_original = WebUtils.getCookie(request,  "_popupSlogan");
        String userId = StringUtils.isEmpty(userCookie_original) ? MD5.getMD5(String.valueOf(System.currentTimeMillis())) : userCookie_original;
        Map<String, Object> resultMap = popupSloganService.getIndexPopupSlogan("web",userId);
        if ((boolean)resultMap.get("success") && StringUtils.isEmpty(userCookie_original)) {
            WebUtils.setCookie(response,"_popupSlogan", userId, 100);
        }
        return resultMap;
    }

    /**
     * 添加弹框点击
     * @param id
     * @return
     */
    @RequestMapping("/web/index/addClick")
    @ResponseBody
    public Map addClick(@RequestParam("id") BigDecimal id) {
        popupSloganDetailService.updateClick(id);
        return ResultUtil.SUCCESS("");
    }
}
